from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ChromeOptions

import time
from PIL import Image
from chaojiying import Chaojiying_Client
import re
from lxml import etree
from xpath import get_info
from save_mongo import save_mongoDB


def add_cookie():
    driver.delete_all_cookies()
    driver.add_cookie({'name': 'TYCID', 'value': 'd0697930fc8311ea81c0fbbfaae7c74a}'})
    driver.add_cookie({'name': 'ssuid', 'value': '3763445571}'})
    driver.add_cookie({'name': '_ga', 'value': 'GA1.2.1525439219.1600745369}'})
    driver.add_cookie({'name': '_gid', 'value': 'GA1.2.1295114731.1606457321}'})
    driver.add_cookie({'name': 'tyc-user-phone', 'value': '%255B%252218306657633%2522%255D}'})
    driver.add_cookie({'name': 'auth_token',
                       'value': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxODMwNjY1NzYzMyIsImlhdCI6MTYwNjQ3MzA5OCwiZXhwIjoxNjM4MDA5MDk4fQ.ebE0_x8q6XsJaIlRI0V6AIZX-ckGGrD36Dpr1LuzwWC7ahWiJ-PhjfG33F01o4OXcyQp7OiXwj1Ych1cA3ukXA}'})
    driver.add_cookie({'name': 'tyc-user-info',
                       'value': '{%22claimEditPoint%22:%220%22%2C%22vipToMonth%22:%22false%22%2C%22explainPoint%22:%220%22%2C%22personalClaimType%22:%22none%22%2C%22integrity%22:%2210%25%22%2C%22state%22:%220%22%2C%22score%22:%22159%22%2C%22announcementPoint%22:%220%22%2C%22messageShowRedPoint%22:%220%22%2C%22bidSubscribe%22:%22-1%22%2C%22vipManager%22:%220%22%2C%22onum%22:%221%22%2C%22monitorUnreadCount%22:%22286%22%2C%22discussCommendCount%22:%221%22%2C%22showPost%22:null%2C%22messageBubbleCount%22:%220%22%2C%22claimPoint%22:%220%22%2C%22token%22:%22eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxODMwNjY1NzYzMyIsImlhdCI6MTYwNjQ3MzA5OCwiZXhwIjoxNjM4MDA5MDk4fQ.ebE0_x8q6XsJaIlRI0V6AIZX-ckGGrD36Dpr1LuzwWC7ahWiJ-PhjfG33F01o4OXcyQp7OiXwj1Ych1cA3ukXA%22%2C%22schoolAuthStatus%22:%222%22%2C%22userId%22:%2237158050%22%2C%22scoreUnit%22:%22%22%2C%22redPoint%22:%220%22%2C%22myTidings%22:%220%22%2C%22companyAuthStatus%22:%222%22%2C%22originalScore%22:%22159%22%2C%22myAnswerCount%22:%220%22%2C%22myQuestionCount%22:%220%22%2C%22signUp%22:%220%22%2C%22privateMessagePointWeb%22:%220%22%2C%22nickname%22:%22%E9%87%8D%E9%98%B3%22%2C%22privateMessagePoint%22:%220%22%2C%22bossStatus%22:%222%22%2C%22isClaim%22:%220%22%2C%22yellowDiamondEndTime%22:%220%22%2C%22yellowDiamondStatus%22:%22-1%22%2C%22pleaseAnswerCount%22:%220%22%2C%22bizCardUnread%22:%220%22%2C%22vnum%22:%220%22%2C%22mobile%22:%2218306657633%22%2C%22riskManagement%22:{%22servicePhone%22:null%2C%22mobile%22:18306657633%2C%22title%22:null%2C%22currentStatus%22:null%2C%22lastStatus%22:null%2C%22quickReturn%22:false%2C%22oldVersionMessage%22:null%2C%22riskMessage%22:null}}}'})
    driver.add_cookie({'name': 'tyc-user-info-save-time', 'value': '1606473096280}'})
    driver.add_cookie({'name': 'csrfToken', 'value': 'ufKAWode_k0S7P6bDJmrpruv}'})
    driver.add_cookie({'name': 'cid', 'value': '22822}'})
    driver.add_cookie({'name': 'bannerFlag', 'value': 'true}'})
    driver.add_cookie({'name': 'jsid', 'value': 'SEO-BAIDU-ALL-SY-000001}'})
    driver.add_cookie({'name': 'ss_cidf', 'value': '1}'})
    driver.add_cookie({'name': 'Hm_lvt_e92c8d65d92d534b0fc290df538b4758', 'value': '1606457321,1606646310,1606646340}'})
    driver.add_cookie({'name': 'CT_TYCID', 'value': 'fc66c0ea68514234a41e9d42f777f2eb}'})
    driver.add_cookie({'name': 'RTYCID', 'value': 'd18a05124a8a40adba89567ab6608840}'})
    driver.add_cookie({'name': 'sensorsdata2015jssdkcross',
                       'value': '%7B%22distinct_id%22%3A%221760851612812d-0f8bb610614157-f7d123e-1327104-1760851612981e%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%221760851612812d-0f8bb610614157-f7d123e-1327104-1760851612981e%22%7D}'})
    driver.add_cookie({'name': 'token', 'value': 'c1f964c420e14ad59831b258ff15ea6b}'})
    driver.add_cookie({'name': '_utm', 'value': 'c0e47a590e674979bcd5d1f6f6524d1b}'})
    driver.add_cookie({'name': 'Hm_lpvt_e92c8d65d92d534b0fc290df538b4758', 'value': '1606666628}'})


def yanzheng():
    print('开始处理验证码。。。。。')
    # 获取验证码窗口模块
    yzm_window = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[3]/div[1]')
    # 截取全图
    driver.save_screenshot('./img/quan.png')
    # 获取验证码窗口模块左上角坐标位置
    location = yzm_window.location
    # 获取验证码窗口模块右下角坐标位置
    size = yzm_window.size
    # 拼接我们要截取的窗口坐标
    crop_parms = (location['x'], location['y'], location['x'] + size['width'], location['y'] + size['height'])
    # print(crop_parms)
    # 使用PIL模块的Image类对图片操作截取验证码图片,保存
    image = Image.open('./img/quan.png')
    image_part = image.crop(crop_parms)
    image_part.save('./img/part.png')
    chaojiying = Chaojiying_Client('zzx865086862', 'zzx19960529', '909508')
    im = open('./img/part.png', 'rb').read()
    result = chaojiying.PostPic(im, '9004')
    # print(result)
    cuowuma = result[0]
    zuobiao = result[1]
    # print(zuobiao)
    if '|' in zuobiao:
        zuobiao_list = str(zuobiao).split('|')
        # print(zuobiao_list)
        for i in zuobiao_list:
            x = int(str(i).split(',')[0])
            y = int(str(i).split(',')[1])
            # print(x, y)
            ActionChains(driver).move_to_element_with_offset(yzm_window, x, y).click().perform()
    else:
        x = int(str(zuobiao).split(',')[0])
        y = int(str(zuobiao).split(',')[1])
        # print(x, y)
        ActionChains(driver).move_to_element_with_offset(yzm_window, x, y).click().perform()
    time.sleep(1.5)
    driver.find_element_by_xpath('//*[@id="submitie"]').click()
    time.sleep(1)
    while True:
        if '我们只是确认一下你不是机器人' in driver.page_source:
            chaojiying.ReportError(cuowuma)
            time.sleep(3)
            yanzheng()
        else:
            break


def cut_img():
    # 获取验证码窗口模块
    yzm_window = driver.find_element_by_xpath('//div[@class="gt_bg gt_show"]')
    # 截取全图
    driver.save_screenshot('./img/quan.png')
    # 获取验证码窗口模块左上角坐标位置
    location = yzm_window.location
    # 获取验证码窗口模块右下角坐标位置
    size = yzm_window.size
    # 拼接我们要截取的窗口坐标
    crop_parms = (location['x'], location['y'], location['x'] + size['width'], location['y'] + size['height'])
    image = Image.open('./img/quan.png')
    image_part = image.crop(crop_parms)
    image_part.save('./img/part.png')


def login():
    driver.get('https://www.tianyancha.com/?jsid=SEM-BAIDU-PZ-SY-20201109-BIAOTI')
    # driver.get('https://www.tianyancha.com/login')
    # 点击登陆
    wait.until(EC.presence_of_element_located(
        (By.XPATH, '//*[@id="web-content"]/div/div[1]/div[1]/div/div/div[2]/div/div[6]/a'))).click()
    wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="title-tab text-center"]/div[2]'))).click()
    wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mobile"]'))).send_keys('18306657633')
    wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="password"]'))).send_keys('yuanle2025')
    time.sleep(1)
    wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="btn -xl btn-primary -block"]'))).click()
    time.sleep(4)


class Res:
    def __init__(self, driver):
        self.url = driver.current_url
        self.text = driver.page_source

def main():
    # 验证码测试url
    detail_url = 'https://antirobot.tianyancha.com/captcha/verify?return_url=https%3A%2F%2Fwww.tianyancha.com%2Fcompany%2F867595458&rnd='
    # url = 'https://www.tianyancha.com/company/2349023170'
    while True:
        # 从redis链表中获取数据
        detail_url = redis_server.rpop('tyc_detail_url')
        detail_url = detail_url.decode('utf-8') if isinstance(detail_url, bytes) else detail_url

        if not detail_url:
            print('redis库中的数据已经全部爬取完成')
            break
        try:
            if redis_server.sadd('tyc_repeat', detail_url):
                # driver.get(str(detail_url)[2:-1])
                driver.get(detail_url)
                driver.implicitly_wait(10)
                if '我们只是确认一下你不是机器人' in driver.page_source:
                    yanzheng()
                # print(f'url--->{driver.current_url}')
                if 'login' in driver.current_url:
                    driver.get(detail_url)
                    driver.implicitly_wait(10)
                # tree = etree.HTML(driver.page_source)
                # if tree:
                res = Res(driver)
                # 提取信息
                all_infos = get_info(res)
                print(all_infos)
                if not all_infos:
                    input('页面错误')
                    raise Exception('页面错误')
                # 保存入数据库
                save_mongoDB(all_infos, 'guangzhou', 1)
            else:
                print(detail_url, '此网站已经爬取过，跳过此网站的爬取。')
            except_num = 0
        except Exception as e:
            except_num = + 1
            print(f"{detail_url} error   {e}")
            redis_server.srem('tyc_repeat', detail_url)
            redis_server.lpush('tyc_exception', detail_url)
            if except_num > 3:
                break


if __name__ == '__main__':
    options = ChromeOptions()
    options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4039.400')
    # options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})  # 不加载图片,加快访问速度
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    driver = webdriver.Chrome(chrome_options=options)
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
          Object.defineProperty(navigator, 'webdriver', {
            get: () => undefined
          })
        """
    })
    options.add_experimental_option('useAutomationExtension', False)
    driver.maximize_window()
    wait = WebDriverWait(driver, 20)
    import redis

    redis_server = redis.Redis(host='127.0.0.1', port='6379')
    login()
    main()
